package chess.utils;

import chess.model.ChessBoardPos;
import chess.model.Session;
import chess.server.NewLineEvent;
import chess.server.NewLineListener;
import chess.server.ServerConnection;

public class ChessBoardParser extends Thread {

	private ChessBoardPos positionsModel;

	private ServerConnection con;

	// private String[][] positions = new String[8][8];

	private String line;

	public ChessBoardParser(ChessBoardPos positionsModel) {
		this.positionsModel = positionsModel;

		System.out.println("PARSER");
		this.start();

	}

	public void run() {
		try {
			sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Session session = Session.getInstance();
		con = session.getConnection();
		con.addNewLineListener(new NewLineListener() {
			@Override
			public void handleNewLineEvent(NewLineEvent e) {
				// positionsModel.setInitialPositions();
				line = e.getLine();
				if (line != null) {
					// if(line.length()>3){
					if (line.substring(0, 4).equals("<12>")) {
						System.out.println("PARSER12:" + line);
						Style12 style12 = new Style12(line);
						positionsModel.setPositions(style12);
					}
					// }
				}
			}
		});
	}

	public static void printArray(String[][] array) {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				System.out.print(array[i][j]);
			}
			System.out.println();
		}
	}

}
